// 移动端菜单切换
const mobileMenuBtn = document.querySelector('.mobile-menu-btn');
const navLinks = document.querySelector('.nav-links');

mobileMenuBtn.addEventListener('click', function() {
    navLinks.classList.toggle('active');
    const menuIcon = document.querySelector('.menu-icon');
    menuIcon.classList.toggle('open');
});

// 平滑滚动
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function(e) {
        e.preventDefault();
        const targetId = this.getAttribute('href');
        if (targetId === '#') return;

        const targetElement = document.querySelector(targetId);
        if (targetElement) {
            window.scrollTo({
                top: targetElement.offsetTop - 80,
                behavior: 'smooth'
            });

            // 如果是移动端，点击后关闭菜单
            if (window.innerWidth <= 768) {
                navLinks.classList.remove('active');
            }
        }
    });
});

// 监听滚动，高亮当前导航项
window.addEventListener('scroll', function() {
    const scrollPosition = window.scrollY;

    // 确定当前页面
    const currentPage = window.location.pathname.split('/').pop();

    // 移除所有导航项的active类
    document.querySelectorAll('.nav-links a').forEach(link => {
        link.classList.remove('active');
    });

    // 为当前页面的导航项添加active类
    if (currentPage === 'index.html') {
        document.querySelector('.nav-links a[href="index.html"]').classList.add('active');
    } else if (currentPage === 'list.html') {
        document.querySelector('.nav-links a[href="list.html"]').classList.add('active');
    } else if (currentPage === 'post.html') {
        document.querySelector('.nav-links a[href="list.html"]').classList.add('active');
    }
});

// 搜索功能
const searchForm = document.querySelector('.search-box form');
if (searchForm) {
    searchForm.addEventListener('submit', function(e) {
        e.preventDefault();
        const searchTerm = this.querySelector('input').value;
        alert(`搜索: ${searchTerm}`);
        // 实际应用中，这里会发送搜索请求到服务器
    });
}

// 评论提交功能
const commentForm = document.querySelector('.comment-form');
if (commentForm) {
    commentForm.addEventListener('submit', function(e) {
        e.preventDefault();
        const name = this.querySelector('#name').value;
        const email = this.querySelector('#email').value;
        const comment = this.querySelector('#comment').value;

        alert(`评论提交成功！\n姓名: ${name}\n邮箱: ${email}\n评论内容: ${comment}`);
        // 实际应用中，这里会发送评论到服务器
        this.reset();
    });
}

// 图片懒加载
document.addEventListener('DOMContentLoaded', function() {
    const lazyImages = document.querySelectorAll('img[data-src]');

    if ('IntersectionObserver' in window) {
        const imageObserver = new IntersectionObserver((entries, observer) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    const image = entry.target;
                    image.src = image.dataset.src;
                    image.removeAttribute('data-src');
                    imageObserver.unobserve(image);
                }
            });
        });

        lazyImages.forEach(image => {
            imageObserver.observe(image);
        });
    } else {
        // 回退方案
        lazyImages.forEach(image => {
            image.src = image.dataset.src;
            image.removeAttribute('data-src');
        });
    }
});

// 页面加载动画
window.addEventListener('load', function() {
    document.body.classList.add('loaded');
});